package sunyu.service.shiro.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sunyu.mapper.shiro.ResourceMapper;
import sunyu.mapper.shiro.pojo.Resource;
import sunyu.service.shiro.ResourceService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {

    @javax.annotation.Resource(name = "resourceMapper")
    private ResourceMapper resourceMapper;

    @Override
    public List<Resource> select(Page page,
                                 Map<String, Object> params) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.isCount());
        }
        return resourceMapper.select(params);
    }

    @Override
    @Transactional
    public Boolean insert(Resource resource) {
        if (resourceMapper.insertSelective(resource) > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public Boolean delete(String id) {
        Resource current = resourceMapper.selectByPrimaryKey(id);
        if (StringUtils.isNotBlank(current.getPid())) {//如果要删除资源的父级不为空，则修复子级
            fixChildrenLevels(current.getPid());
        } else {//如果父级为空，则子集的levels需要变更为0
            Map<String, Object> params = new HashMap<String, Object>() {{
                put("pid", current.getId());
            }};
            List<Resource> children = resourceMapper.select(params);
            if (children != null && children.size() > 0) {
                for (Resource r : children) {
                    r.setLevels(0);
                    resourceMapper.updateByPrimaryKeySelective(r);

                    fixChildrenLevels(r.getId());
                }
            }
        }

        if (resourceMapper.deleteByPrimaryKey(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Resource selectById(String id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Boolean update(Resource resource) {
        if (resourceMapper.updateByPrimaryKeySelective(resource) > 0) {

            fixChildrenLevels(resource.getId());

            return true;
        }
        return false;
    }

    /**
     * 修复当前资源下属资源的级别
     *
     * @param id
     */
    private void fixChildrenLevels(String id) {
        Resource current = resourceMapper.selectByPrimaryKey(id);//当前节点
        if (current != null) {
            Map<String, Object> params = new HashMap<String, Object>() {{
                put("pid", current.getId());
            }};
            List<Resource> children = resourceMapper.select(params);//子节点
            if (children != null && children.size() > 0) {
                for (Resource r : children) {
                    r.setLevels(current.getLevels() + 1);
                    resourceMapper.updateByPrimaryKeySelective(r);

                    fixChildrenLevels(r.getId());//继续修复子节点
                }
            }
        }
    }

    @Override
    public Long count(Map<String, Object> params) {
        return PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                resourceMapper.select(params);
            }
        });
    }
}
